package com.appspot.yourvirtualgymtrainer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.logging.Level;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Entity;

public class PlanServlet extends BaseServlet {
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			  throws ServletException, IOException {
		
		HttpSession sess = req.getSession(true);
		PrintWriter out = resp.getWriter();

		try{
			if(!sess.getAttribute("isLogged").equals("no")) {
				super.doGet(req, resp);
				String login = (String) sess.getAttribute("isLogged");
				
				Entity profil = Profil.getProfil(login);
				if(profil != null){
					//login
					String planString = Plan.return_Json(login);
					out.println(planString);
					System.out.println(planString);
				}	
			} else {
				sess.setAttribute("isLogged", "no");
				resp.setContentType("text/plain; charset=utf-8");
				out.println("niezalogowany");
			}
		} catch(NullPointerException e) {
			resp.setContentType("text/plain; charset=utf-8");
			out.println("niezalogowany");
		} 
		
	 }
	
	
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
		    throws ServletException, IOException {
		//sprawdzenie zalogowania
		HttpSession sess = req.getSession(true);
		PrintWriter out = resp.getWriter();
		try{
			if(!sess.getAttribute("isLogged").equals("no")) {
				super.doGet(req, resp);
				String login = (String) sess.getAttribute("isLogged");
				
				Entity profil = Profil.getProfil(login);
				if(profil != null){
			 
					 Map<String, Object> parameters = profil.getProperties();
					
				
					
					 int determ=Integer.parseInt((String)parameters.get("intensywnosc"));
					 int staz=Integer.parseInt((String)parameters.get("practice"));
					 String data=(String)parameters.get("birthdate");
					 String[] result = data.split("/");
					 Calendar now = Calendar.getInstance();
					 
					 if(result[2] == null) {
						 result[2] = "1990"; 
					 }
					 int age=now.get(Calendar.YEAR)-Integer.parseInt(result[2]);
					 
			//wywolanie algorytmu		
			Plan.getInstance().algorithm(login,age,staz, "tydzien",(String)parameters.get("czas"),(String)parameters.get("partia"), (String)parameters.get("cel"), determ);

			//testowy
			//Plan.getInstance().algorithm(login,12,12, "tydzien","4-6h","ogolny", "inny", 1);	  		
					 
					 
				}	
			} else {
				sess.setAttribute("isLogged", "no");
				resp.setContentType("text/plain; charset=utf-8");
				out.println("niezalogowany");
			}
		} catch(NullPointerException e) {
			resp.setContentType("text/plain; charset=utf-8");
			out.println("nullpointer");
		} 
			
		}
}
